package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.CustomerEvaluate;
import com.weipai.springboot.model.SpecialOffer;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.EvaluateDao;
import com.weipai.springboot.module.service.EvaluateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/28 19:38
 */
@Service
public class EvaluateServiceImpl implements EvaluateService{
    
    private static final Logger log = LoggerFactory.getLogger(EvaluateServiceImpl.class);
    
    @Autowired
    EvaluateDao evaluateDao;

    //分页查询
    @Override
    public Response selectAllByPage(Integer page, Integer size, String draw) {
        Integer start = (page-1) * size;
        List<Map<String, Object>> evaluates = evaluateDao.selectAllByPage(start, size);
        Integer totalNum = evaluateDao.selectAllByPageAmount();
        PageResult<Map<String, Object>> pageList = new PageResult<>(evaluates,page,size,totalNum, draw);
        Response response = new Response();
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }

    //审核
    @Override
    public Response evaluateAudit(Integer id, Integer status) {
        Response response = new Response();
        if(evaluateDao.evaluateAudit(id, status)==1){
            response.setCode(RetCode.SUCCESS);
        }else{
            log.info("审核评论不通过，id:{}"+id);
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //回复评论
    @Override
    public Response replyEvaluate(Integer id, String replyText) {
        Response response = new Response();
        if(evaluateDao.replyEvaluate(id, replyText)==1){
            response.setCode(RetCode.SUCCESS);
        }else{
            log.info("回复评论不通过，id:{}"+id);
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //获取评论内容
    @Override
    public Response getEvaluateInfo(Integer id) {
        Response response = new Response();
        Map<String, Object> map = evaluateDao.getEvaluateInfo(id);
        if(null!=map && map.size()>0){
            response.setCode(RetCode.SUCCESS);
            response.setData(map);
        }else{
            log.info("未获取到此评论的内容，评论id：{}"+id);
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    //删除
    @Override
    public Response batchDeleteById(String[] id) {
        Response response = new Response();
        if(evaluateDao.batchDeleteById(id)>0){
            response.setCode(RetCode.SUCCESS);
        }else{
            log.info("删除评论不通过，id:{}"+id.toString());
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //前端调用，某一货品的所有评论，分页查询
    @Override
    public Response getEvaluateBySpuNumber(Integer page, Integer size, String spuNumber, String draw, Integer judgeValue) {
        Integer start = (page-1) * size;
        List<Map<String, Object>> evaluates = evaluateDao.getEvaluateBySpuNumber(start, size, spuNumber, judgeValue);
        Integer totalNum = evaluateDao.getEvaluateAmountBySpuNumber(spuNumber);
        PageResult<Map<String, Object>> pageList = new PageResult<>(evaluates,page,size,totalNum, draw);
        Response response = new Response();
        response.setCode(RetCode.SUCCESS);
        response.put("pageList", pageList);
        return response;
    }
}
